package com.kaluli.project.service;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.kaluli.project.domain.entity.KllMq;
import com.kaluli.project.domain.repository.KllMqRepository;

@Service
@Configuration
@Transactional
public class KllMqService extends CommonService{

	@Autowired
	private RedisTemplate<Serializable, Serializable> redisTemplate;
	
	@Autowired
	private Environment env;

	@Autowired
	private KllMqRepository kllMqRepository;
	
	

	public List<KllMq> getAll() {
		return kllMqRepository.findAll();
	}

	public List<KllMq> getByOrderNumber(String orderNumber,String msgchannel) {
		List<KllMq> gjOrderInfo = kllMqRepository.getByOrderNumber(orderNumber,msgchannel);
		
		return gjOrderInfo;
	}
	
	public List<KllMq> findByOrderNumber(String orderNumber) {
		List<KllMq> gjOrderInfo = kllMqRepository.findByOrderNumber(orderNumber);
		
		return gjOrderInfo;
	}
	
	public List<KllMq> getListByChannel(String orderNumber,String msgchannel) {
		List<KllMq> mqlist = kllMqRepository.findMqOrderNumber(orderNumber,msgchannel);
		
		return mqlist;
	}
	
	
	public List<KllMq> findMqlistOnehour() {
		List<KllMq> gjOrderInfo = kllMqRepository.findMqlistOnehour();
		return gjOrderInfo;
	}

	public KllMq findByid(Long id) {
		return kllMqRepository.findById(id);
	}
	
	public Long save(final KllMq kllMq){
		final Long id = (Long)kllMqRepository.save(kllMq);
		/* redisTemplate.execute(new RedisCallback<Object>() {  
		        @Override  
		        public Object doInRedis(RedisConnection connection)  
		                throws DataAccessException {  
		        	
		            connection.set(  
		                    redisTemplate.getStringSerializer().serialize(  
		                            "kll.mq.id." + id),  
		                    redisTemplate.getStringSerializer().serialize(kllMq.toString()));  
		            return null;  
		        }  
		    });  */
		
		return id;
	}
	
	public void updateMq(KllMq kllMq){
		kllMqRepository.updateMq(kllMq);
	}

	public String addDays(int days) {
		String now = null;
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf = new SimpleDateFormat("MM");
		java.util.Calendar calstart = java.util.Calendar.getInstance();
		calstart.add(java.util.Calendar.DAY_OF_WEEK, days);
		String str = sDateFormat.format(calstart.getTime()).substring(5, 7);
		Date date = null;
		try {
			date = sdf.parse(str);
		} catch (ParseException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		sdf = new SimpleDateFormat("MMMMM", Locale.US);
		String time1 = sDateFormat.format(calstart.getTime());
		now = sdf.format(date) + " " + time1.substring(8, 10) + ", " + time1.substring(0, 4);
		return now;
	}
	// private static final SimpleDateFormat dateFormat = new
	// SimpleDateFormat("HH:mm:ss");

	
	public KllMq findByMsgId(String msgId){
		KllMq kllMq = null;
		if(msgId!=null){
			kllMq =  kllMqRepository.findByMsgId(msgId);
		}
		return kllMq;
	}
	
	public String getMsgId(String orderNumber,String msgChannel,String pattern){
		List<KllMq> lists = kllMqRepository.getByOrderNumber(orderNumber, msgChannel);
		int intMsgId = lists.size();
		intMsgId++;
		DecimalFormat df = new DecimalFormat(pattern);
	    return df.format(intMsgId);
		
	}
//	@Scheduled(fixedRate=360000)
//	public void sendGjOrder() {
//		String ftpurl = env.getProperty("ftp.url");
//		String username = env.getProperty("ftp.username");
//		String password = env.getProperty("ftp.password");
//		int port = new Integer(env.getProperty("ftp.port"));
//		String orderin = env.getProperty("ftp.order.in");
//		String orderout = env.getProperty("ftp.order.out");
//		String local = env.getProperty("ftp.local");
//		
//		XmlBuilder xb = XmlBuilder.getInstance();
//		//xb.addHeader(key, value)
//	}
}
